package com.crhms.seabow.controller;

import com.crhms.seabow.entity.Subtitle;
import com.crhms.seabow.entity.Word;
import com.crhms.seabow.service.SubtitleService;
import com.crhms.seabow.service.WordService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/word")
public class WordController {

    @Autowired
    private WordService wordService;

    @RequestMapping("/getWords")
    public List<String> getWords(Integer lessonId){

        List<Word> subtitles = wordService.getWords(lessonId);

        List<String> words = subtitles.stream().map(w -> {

            StringBuilder result = new StringBuilder(w.getKana());

            if (!StringUtils.isEmpty(w.getChinese())){
                result.append(String.format("(%s)",w.getChinese()));
            }

            if (!StringUtils.isEmpty(w.getCategory())){
                result.append(String.format("[%s]",w.getCategory()));
            }

            if (!StringUtils.isEmpty(w.getComment())){
                result.append(String.format(" %s",w.getComment()));
            }
            return result.toString();
        }).collect(Collectors.toList());

        return words;
    }
}
